<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Running extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	
	public function index()
	{
		$data=$this->getRunningData();
		//var_dump($data);
		echo json_encode($data);
	}
	public function getUserData(){
		$dsn = "mysql:host=localhost;dbname=running;port=3306;charset=utf8mb4";

            try {
                $conn = new PDO($dsn, 'root', 'wxd5f88bf66c19734a');
            } catch (PDOException $e) {
                throw new Exception(Constants::E_CONNECT_TO_DB . ': '. $e->getMessage());
            }
		$sql = 'select name,sex,job_name,picture,introduce from sys_user ';
		$res=$conn->prepare($sql);//准备查询语句  
		$res->execute(); 
		$result=$res->fetchAll(PDO::FETCH_ASSOC);
		echo json_encode($result);
	}
	private function getRunningData(){
		$dsn = "mysql:host=localhost;dbname=running;port=3306;charset=utf8mb4";

            try {
                $conn = new PDO($dsn, 'root', 'wxd5f88bf66c19734a');
            } catch (PDOException $e) {
                throw new Exception(Constants::E_CONNECT_TO_DB . ': '. $e->getMessage());
            }
		$sql = 'select name,sex,job_name,picture,introduce from sys_user a join g_run_data b on a.id=b.user_id';
		$res=$conn->prepare($sql);//准备查询语句  
		$res->execute(); 
		$result=$res->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}
}
